/* GET home page. */

var path = require('path');



var fs = require('fs');

var mslibrary = require("ms-common-library");




module.exports = function (UC, source, config, ArFor, cache, package) {

    var UserControl = UC;
    var email = config.email;
    var install = function (Callback) {
        var _path = path.join(config.Files.Temp, '*');
        mslibrary.rmdir(_path, function () {
            Callback()
        }, true);
    }
    var adduser = function (app, Callback) {

        var openjson = config.openjson;
        openjson(path.join(package.dirname, "install.json"), function (error, db) {
            if (error) {
                db = {}
            };
            var jschema = config.jschema;
            jschema(package, db, function () {
                var definfo = {
                    "username": "admin",
                    "name": "超级权限",
                    "password": "fcea920f7412b5da7be0cf42b8c93759",
                    "roles_id": "1",
                    "user_id": "1",
                    "Updated_Date": "2018-12-24 21:43:14",
                    "Creation": "2017-10-08 20:31:30.000",
                    "icon": "3",
                    "state": "1",
                    "email": app.email,
                    Coinkey: '{Coinkey}'
                };
                new source({
                    "SQL": "INSERT INTO [user] (username,name,password,roles_id,user_id,Updated_Date,Creation,icon,state,email,Coinkey) values ('{username}','{name}','{password}','{roles_id}','{user_id}',datetime('now','+8 hour'),datetime('now','+8 hour'),'{icon}','{state}','{email}','{Coinkey}');",
                    "type": "add"
                }, definfo, function (obj) {
                    Callback()

                })
            })
        })
    }
    var router = function (req, res, next) {
        var body = req.body;
        config.language = body.language;
        var key = req.sessionID;
        if (body.type == "1") {
            install(function () {
                /// config.db.Files=':memory:';
                config.install = true;
                adduser({
                    email: "musavips@163.com"
                }, function () {
                    config.InitConfig(function () {
                        setTimeout(function () {
                            res.Json({
                                error: 0
                            });
                        }, 200);
                    })
                })
            })

        } else if (body.Code == "true") {

            UserControl.setvalue(key, {});

            var VerificationCode = parseInt(Math.random() * 9) + '' + parseInt(Math.random() * 9) + '' + parseInt(Math.random() * 9) + '' + parseInt(Math.random() * 9) + '' + parseInt(Math.random() * 9) + '' + parseInt(Math.random() * 9) + '';

            // VerificationCode = 123456;

            var db = {
                "view": {
                    id: "XMKEA9KCGX",
                    file: './jviews/email/install.ejs'
                },
                pass: body.password,
                email: body.email,
                to: body.email,
                subject: '部署系统环境认证',
                options: {
                    title: '部署系统环境认证',
                    VerificationCode: VerificationCode
                }
            };


            var _email = new email(db);
            _email.sendMailView(db, res, function (off, error) {
                console.log(off)
                console.log(error)
                if (off == true) {
                    res.Json({
                        error: 103
                    });
                } else {
                    UserControl.SetParamet(key, 'install', {
                        email: body.email,
                        password: body.password,
                        VerificationCode: VerificationCode
                    });
                    res.Json({
                        error: 0
                    });
                }
            })
        } else if (body.install == "true") {
            var app = UserControl.GetParamet(key, 'install');
            if (app && app.VerificationCode == body.VerificationCode) {
                var setdbinfo = function () {
                    adduser(app, function () {
                        new source({
                                "SQL": "UPDATE [config] SET email='{email}' ,pass='{password}'   WHERE id = 1;",
                                "type": "edit"
                            }, app,
                            function (obj) {
                                if (obj == null) {
                                    setTimeout(function () {
                                        setdbinfo()
                                    }, 200);
                                } else {
                                    res.Json({
                                        error: 0
                                    });
                                }
                            });

                    });
                };

                install(function () {
                    config.install = true;
                    config.InitConfig(function () {
                        setTimeout(function () {
                            setdbinfo()
                        }, 200);
                    })
                })

            } else {
                res.Json({
                    error: 101
                });
            }
        } else {

            res.Json({
                error: 101
            });
        }
    }
    return router;

}